package com.ken.apollo.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

@Controller
public class PagesController {

    @GetMapping("/backs/{goPage}")
    public String goPage(@PathVariable String goPage, HttpServletRequest request, Model model){
        model.addAttribute("contextPath", request.getContextPath());
        return "backs/" + goPage;
    }


    @GetMapping("/{goPage}")
    public String goPages(@PathVariable String goPage, HttpServletRequest request, Model model){
        model.addAttribute("contextPath", request.getContextPath());
        return goPage;
    }

    @GetMapping(value = "favicon.ico")
    public ResponseEntity<Resource> favicon() throws IOException {
        Resource favicon = new ClassPathResource("static/favicon.ico");
        if (favicon.exists()) {
            return ResponseEntity.ok()
                    .contentType(MediaType.valueOf("image/x-icon"))
                    .body(favicon);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
